import numpy as np import pandas as pd import matplotlib.pyplot as plt from numpy.polynomial.polynomial import Polynomial # Read the S&P 500 data df = pd.read_csv('/path/to/your/csv/file.csv') # Filter the data to start from March 4, 1957 df_1957 = df[df['Date'] >= '1957-03-04'].reset_index(drop=True) # Convert the dates to numerical values (number of days since March 4, 1957) x_data = np.arange(len(df_1957)) # Normalize the S&P 500 data to start from 1 normalized_s_1957 = df_1957['Close'] / df_1957['Close'].iloc[0] # Fit a first-degree polynomial (a line) to the logarithmic data p = Polynomial.fit(x_data, np.log(normalized_s_1957), 1) # Generate y-values based on the fit y_fit_log = p(x_data) # Plotting the logarithmic graph and the fitted line plt.figure(figsize=(15, 6)) plt.semilogy(x_data, normalized_s_1957, label='Normalized Actual S&P 500 (Log scale)', color='b') plt.semilogy(x_data, np.exp(y_fit_log), label=f'Fitted Line', color='r') plt.title('Logarithmic Plot of the Normalized Actual S&P 500 and Fitted Line (Starting from March 4, 1957)') plt.xlabel('Number of Days') plt.ylabel('Close Price (Log Scale, Normalized)') plt.legend() plt.grid(True) plt.show() # Show the coefficients of the fitted line print("Coefficients of the fitted line:", p.convert().coef)